<!--
 * @Description: 服务订单新增表单
 * @Version: 1.0
 * @Author: Yuanjy
 * @Date: 2020-03-31 12:04:35
 * @LastEditors: Yuanjy
 * @LastEditTime: 2020-04-26 15:56:49
 -->
<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" :title="isAdd ? '新增服务订单' : '编辑服务订单'" width="600px">
    <el-form ref="form" :model="form" :rules="formRules" size="small" label-width="100px">
      <el-form-item label="用户姓名" prop="delivery_name">
        <el-input v-model="form.delivery_name" style="width:80%" />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="form.phone" style="width:80%" />
      </el-form-item>
      <el-form-item label="来源渠道" prop="channel_code">
        <treeselect v-model="form.channel_code" :options="secondChannels" :flatten-search-results="true" :disable-branch-nodes="true" :show-count="true" style="width:80%" placeholder="请选择渠道" />
        <!-- <el-cascader expand-trigger="hover" class="user-input" :options="secondChannels" v-model="channelCodes"  clearable @change="channelChange" placeholder="请选择渠道"></el-cascader> -->
      </el-form-item>
      <el-form-item label="订单类型" prop="order_type">
        <el-select v-model="form.order_type" clearable placeholder="类型" class="filter-item" style="width:80%">
          <el-option v-for="(type, index) in types" :key="index" :label="type" :value="index"/>
        </el-select>
      </el-form-item>
      <el-form-item label="宝贝详情">
        <el-input v-model="form.goods_info" style="width:80%" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  components: { Treeselect },
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    secondChannels: {
      type: Array,
      required: true
    },
    types: {
      type: Object,
      required: true
    },
    sup_this: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      loading: false, 
      dialog: false,
      channelCodes: [],
      form: {
        id: '',
        delivery_name: '',
        channel_code: null,
        phone: '',
        order_type: '',
        goods_info: '',
      },
      formRules: {
        delivery_name: [
          { required: true, message: '请输入用户姓名', trigger: 'blur' }
        ],
        channel_code: [
          { required: true, message: '请选择渠道信息', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
        ],
        order_type: [
          { required: true, message: '请选择订单类型', trigger: 'blur' }
        ],
      }
    }
  },
  mounted() {
    // this.initParams()
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    channelChange(channel) {
      this.form.channel_code = channel[1]
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else this.doEdit()
        } else {
          console.log('error')
          return false
        }
      })
    },
    doAdd() {
      this.$api.order.service
        .add({
          data: this.form
        })
        .then(res => {
          this.resetForm()
          this.$notify({
            title: '添加成功',
            type: 'success',
            duration: 2500
          })
          this.loading = false
          this.$parent.$parent.init()
        }).catch(err => {
          this.loading = false
          console.log(err.response.data.message)
        })
    },
    doEdit() {
      this.$api.order.service
        .edit({
          data: this.form
        })
        .then(res => {
          this.resetForm()
          this.$notify({
            title: '修改成功',
            type: 'success',
            duration: 2500
          })
          this.loading = false
          this.sup_this.init()
        }).catch(err => {
          this.loading = false
          console.log(err)
        })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.channelCodes = []
      this.form = {
        id: '',
        delivery_name: '',
        channel_code: '',
        phone: '',
        order_type: '',
        goods_info: '',
      }
    }
  }
}
</script>

<style scoped>

</style>
